package com.fingard.dsp.bank.directbank.srcb01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Fantx 2019/12/30
 *
 */
public class SRCB01Balance extends SRCB01Base implements IBalance {

    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        //创建返回对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            //请求登陆
            String[] loginResult = login();
            if (loginResult[0].length() != 0) {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(loginResult[1]);
                retAts.writeEnd();
                return retAts;
            }

            //新建发送银行报文
            XmlTextWriter xmlWriterStep1 = new XmlTextWriter();
            String transCode = getBankConfig().getMapValueByKey("transCode", "7506");
            writeCommonHeader(xmlWriterStep1, transCode, "");

            //报文体参数
            while (p_req7506.readRecord()) {

                List<String[]> paramStep1 = new ArrayList<String[]>();
                paramStep1.add(new String[]{"AcctNo", p_req7506.readedDetail.actNum});  //账号
                paramStep1.add(new String[]{"AcctCurr", getBankConfig().getMapValueByKey("currencyMapping", p_req7506.readedDetail.actCur)});                         //币种
                writeBodyParams(xmlWriterStep1, paramStep1);

                String[] result = sendToBank(xmlWriterStep1.toXmlString());
                if (result[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(result[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpRetCode = tmpBkReader.getFirstText("/response/head/ret_code");
                        if ("000000".equals(tmpRetCode)) {
                            while (tmpBkReader.read("/response/body")) {
                                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.respInfo = "success";
                                tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("acct_no");
                                tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("balance");
                                tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("available_balance");
                                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                            }
                        } else {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(tmpBkReader.getFirstText("/response/head/err_msg"));
                        }
                    } else {
                        WriteBankLogLn("步骤一发送成功，银行返回报文解析失败，报文内容为：" + result[1]);
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(result[1]);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(result[1]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            WriteBankLogLn("交易失败：发送异常" + e);
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
